//index.js
//获取应用实例
const $vm = getApp()

const cache = Object.create(null)

const {decodeHtml,parseNews} = $vm.utils


Page({
    _isLoading:false,
    data: {
      swiperList: [],
      articles: [],
      categoryTabs: [],
      currentTab: 0
      
    },
    onShow() {
      if ($vm.globalData.categoryChanged) {
        $vm.utils.getCategorys().then(res => this.setData({

          categoryTabs: res
        }))

        $vm.globalData.categoryChanged = false

      }
    },
    onReady(){
        this.getNewsList()
    },
    onLoad: function() {

    },

    // 页面相关事件处理函数--监听用户下拉动作，下拉刷新
    onPullDownRefresh(){
        // 刷新页面，清空当前的缓存，重新获取
        var chid = this.data.currentTab
        // 命中缓存
        if(cache[chid]){
            cache[chid] = {slides:[],news:[],page:0,time:Date.now()}
        }
        this.getNewsList(chid)
    },
    // 到达底部，重新加载
    onReachBottom(){

        this.getNewsList(this.data.currentTab,1)
    },
    // 切换当前选择的分类
    changeCategory(event){
        var chid = event.target.dataset.id
        console.log("===" + chid);

        // 获取ccurrentTab.没有切换分类
        if(this.data.currentTab === chid){
            return false
        }
        this.setData({ currentTab:chid })
        this.getNewsList(chid,0)
    },
    getNewsList(chid = "T1348647909107" ,page = 0){


        if(!cache[chid]){
            // 新内容
            cache[chid] = {slides:[],news:[],page:0,time:Date.now()}
        }
        var infos = cache[chid]
        // 获取下一页数据
        if(page){
            // 加载中。无法触发
            if(this._isLoading){
                return false;
            }
            infos.page += 1
        }else{
            // 直接从缓存中取出
            if(infos.news.length){
                this.setData({
                    swiperList:infos.slides,
                    articles:infos.news
                })
                return false
            }
        }

        $vm.utils.getCategorys().then(res => this.setData({

          categoryTabs: res
        }))
        // console.log("==infos=" + categoryTabs);

        // console.log("==="  );


        // if (chid == ' 0') { 
        //   child ="T1348647909107"  ;
        // }

      
        this._isLoading = true
            // https://c.m.163.com/nc/article/headline/
        $vm.utils.get('https://c.m.163.com/nc/article/headline/'
          , { chid: chid,page:infos.page}).then(res => {
            this._isLoading = false
            var { T1348647909107 } = res ;
 
            
           
            var ress = JSON.stringify(res) ;
            // console.log(ress);
            var resss = ress.replace(String(chid),'data_array');
            // data_array = resss ;
            // JSON.parse(resss) ;
            var array = [];
            // console.log(JSON.parse(resss).data_array);

            for (var i = 0; i < 18;i++){
              array.push(JSON.parse(resss).data_array[i]);
            }
            // console.log(JSON.parse(resss));
                // 新闻管理
                // style 3下面三张图，1,下面一张大图，2.普通模式，0.无图模式
            if (array.length>0){
                    // infos.news = []
              var datas = parseNews(array)
                    infos.news.push(...datas)
              }
                // 轮播管理
            console.log("===chid===" + String(chid))
            if (String(chid) ==  "T1348647909107") {
            if (array && array.length){
                    // infos.slides = []
              var banners = array.map(news => {
                        return {
                            id:news.id,
                            title:news.title,
                            image:news.imgsrc,
                            style: news.votecount
                        }
                    })
                    infos.slides.push(...banners)
                }
                this.setData({
                    swiperList:infos.slides,
                    articles:infos.news
                })
            }
            
        }).catch(err =>console.log(err))

    },
    manageTabs(){
        wx.navigateTo({
            url:'/pages/news/manage'
        })
    }
})